var express = require('express');
var router = express.Router();

/* GET home page. */
router.get("/",async(req,res)=>{
    res.render("接口请求成功")
})
// 获取登录信息
//登录验证
router.get("/login", async (req, res) => {
    console.log('登录');
    let username = req.query.username;
    let password = req.query.password;
    let tell = req.query.tell;
    let telCode = req.query.getCode;
    let data = null;

    if (username != 'null') {
        data = await studentModel.find({ account: username });
        console.log(data);
        let AccessToken = createTokenCheck.getToken("user", "1800s");
        let RefreshToken = createTokenCheck.getToken("qlb", "259200s");
        if (data.length) {
            if (data[0].password === password) {
                res.send({
                    code: 200,
                    data,
                    msg: "登录成功",
                    AccessToken,
                    RefreshToken,
                });
            } else {
                res.send({
                    code: 201,
                    msg: "密码错误",
                });
            }
        } else {
            res.send({
                code: 403,
                msg: "用户名错误",
            });
            console.log(username);
        }
    } else {
        data = await usersModel.find({ tell: tell });
        let AccessToken = createTokenCheck.getToken("user", "10s");
        let RefreshToken = createTokenCheck.getToken("qlb", "60s");
        if (data.length) {
            console.log(tell);
            if (telCode !== str) {
                console.log(str);
                res.send({
                    code: 404,
                    msg: "验证码错误",
                });
            } else {
                res.send({
                    code: 200,
                    data,
                    msg: "登录成功",
                    AccessToken,
                    RefreshToken,
                });
            }
        } else {
            res.send({
                code: 405,
                msg: "手机号未注册",
            });
        }
    }
});



module.exports = router;
